package com.molyfun.parents.activity.classroom;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.common.ChooseDateActivity;
import com.molyfun.parents.activity.schoolonline.homework.view.DateChooseView;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.CourseListResponse;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.uievent.UIEventListener;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.utils.TimeUtils;
import com.molyfun.parents.view.calendar.MaterialCalendarView;
import com.molyfun.parents.view.swipeRefreshLayout.SwipeRefreshLayout;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 我的课程-小组课列表
 */

public class TeamClassListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener, View.OnClickListener, UIEventListener {

    @InjectView(R.id.base_title)
    TextView mTitle;
    @InjectView(R.id.base_title_left)
    ImageView mTitleBack;
    @InjectView(R.id.course_list)
    RecyclerView mRecyclerView;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmptyLayout;
    @InjectView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;

    @InjectView(R.id.date_choose_layout)
    DateChooseView mDateChooseView;

    private LinearLayoutManager mLayoutManager;
    private ClassCourseListAdapter mCourseAdapter;

    protected int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;

    public static void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, TeamClassListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classroom_course_list);
        ButterKnife.inject(this);

        mTitleBack.setOnClickListener(this);

        getSupportActionBar().setTitle("");

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);

        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mCourseAdapter = new ClassCourseListAdapter(this);
        mRecyclerView.setAdapter(mCourseAdapter);

        mTitle.setText("班级课(" + TimeUtils.getQueryDate() + ")");
        changeDate(TimeUtils.getQueryDate());

        MFPApplication.getInstance().getEventController().addUIEventListener(EventDispatcherEnum.UI_EVENT_DATE_CHANGE, this);


        mDateChooseView.setDateChooseViewCallback(new DateChooseView.DateChooseViewCallback() {
            @Override
            public void onDayPreClick() {
                changeDate(TimeUtils.getPreDate(mCurrentDate));
            }

            @Override
            public void onDateClick() {
                ChooseDateActivity.startActivity(TeamClassListActivity.this, mCurrentDate, MaterialCalendarView.PageIdentify.PageMineCourse);
            }

            @Override
            public void onDayNextClick() {
                changeDate(TimeUtils.getNextDate(mCurrentDate));
            }
        });
        String todayDate = TimeUtils.getQueryDate();
        mDateChooseView.setDate(todayDate);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
        }
    }

    private String mCurrentDate = TimeUtils.getQueryDate();

    private void changeDate(String date) {
        mCurrentDate = date;
        mTitle.setText("班级课(" + mCurrentDate + ")");
        mCurrentPage = RestClient.DEFAULT_PAGE;
        mDateChooseView.setDate(date);
        getCourseList(date);
        Const.CURRENT_COURSE_DAY_STR = date;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MFPApplication.getInstance().getEventController().removeUIEventListener(EventDispatcherEnum.UI_EVENT_DATE_CHANGE, this);
    }

    private void getCourseList(final String date) {
        showProgressDialog();
        Call<CourseListResponse> call = MFPApplication.getInstance().getRestClient().getUserCourseList(date, mCurrentPage);
        call.enqueue(new Callback<CourseListResponse>() {
            @Override
            public void onResponse(Call<CourseListResponse> call, Response<CourseListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null && response.body().getData().getRecords() != null) {
                    if(mCurrentPage == RestClient.DEFAULT_PAGE){
                        if(response.body().getData().getRecords().size() > 0){
                            mCourseAdapter.refreshData(response.body().getData().getRecords());
                            mHasMoreData = true;
                        }else{
                            mHasMoreData = false;
                            mCourseAdapter.clearAll();
                        }
                    }else{
                        if(response.body().getData().getRecords().size() > 0){
                            mHasMoreData = true;
                        }else{
                            mHasMoreData = false;
                        }
                        mCourseAdapter.addAll(response.body().getData().getRecords());
                    }

                }
            }

            @Override
            public void onFailure(Call<CourseListResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });

    }

    @Override
    public void onRefresh() {
        mCurrentPage = RestClient.DEFAULT_PAGE;
        getCourseList(mCurrentDate);
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setLoading(false);
    }

    @Override
    public void onLoad() {

        if(mHasMoreData){
            mCurrentPage ++;
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            getCourseList(mCurrentDate);
        }else{
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
        }

    }

    @Override
    public void handleUIEvent(Message msg) {

        switch (msg.what) {
            case EventDispatcherEnum.UI_EVENT_DATE_CHANGE:
                String date = (String) msg.obj;
                changeDate(date);
                break;
        }
    }

}